/**
 * 
 */
package com.yst.metho.web.application.components.listingview;

import java.text.MessageFormat;

import nextapp.echo2.app.Button;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Table;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import nextapp.echo2.app.event.ChangeEvent;
import nextapp.echo2.app.list.ListSelectionModel;

import com.yst.metho.web.application.SuiviProdApp;
import com.yst.metho.web.application.components.GenericEditWindow;
import com.yst.metho.web.application.components.msgpane.ConfirmationPane;
import com.yst.metho.web.application.interfaces.IListingViewModel;
import com.yst.metho.web.application.utils.ExceptionMgt;
import com.yst.suivi.data.rights.Right;

/**
 * @author perso
 * 
 */
public abstract class AbstractConfirmationAction extends ToolbarAction
{

	protected IListingViewModel mModel;

	protected IConfirmationAction mActionToPerform;

	private String mActionCommand;

	protected Table mTable;

	private String mConfirmationMsg;

	/**
	 * @param text
	 */
	public AbstractConfirmationAction(Right r, String text, String actionCommand, String confirmationMsg)
	{
		super(r, text);
		mActionCommand = actionCommand;
		mConfirmationMsg = confirmationMsg;
	}

	public void actionPerformed(ActionEvent e)
	{
		if (mActionCommand.equals(e.getActionCommand()))
		{
			Object[] params = new Object[1];
			params[0] = mActionToPerform.getConfirmationMessageParameter();
			Button btn = new Button("Oui");
			btn.addActionListener(new ActionListener()
			{

				public void actionPerformed(ActionEvent arg0)
				{
					try
					{
						mActionToPerform.doAction();
						mModel.refresh();
						mTable.getSelectionModel().clearSelection();
					}
					catch (Exception e)
					{
						ExceptionMgt.manage(e);
					}
				}

			});
			GenericEditWindow window = new GenericEditWindow(new ConfirmationPane(MessageFormat.format(mConfirmationMsg, params)), "Confirmation", btn);
			window.setHeight(new Extent(150, Extent.PX));
			window.setCancelMsg("Non");
			SuiviProdApp.getApp().getDefaultWindow().getContent().add(window);
		}
	}

	public void stateChanged(ChangeEvent arg0)
	{
		ListSelectionModel model = (ListSelectionModel) arg0.getSource();
		setEnabled(!model.isSelectionEmpty());
		mActionToPerform.setSelection(arg0.getSource());
		if (!model.isSelectionEmpty())
		{
			setEnabled(mActionToPerform.isEnabled());
		}

	}

}
